// noinspection JSUnresolvedReference

// 初始化Tab
document.addEventListener('DOMContentLoaded', function () {
    const tabs = document.querySelectorAll('.nav-link');
    tabs.forEach(function (tab) {
        tab.addEventListener('click', function () {
            const activeTab = document.querySelector('.nav-link.active');
            if (activeTab) {
                activeTab.classList.remove('active');
            }
            this.classList.add('active');
        });
    });
});

const api = '/api/system';

$(document).ready(function () {
    // 停止服务回调接口
    $('#stop-button').click(function () {
        $.ajax(api, {type: 'DELETE'});
    });

    // 初始化首页信息
    $.get(api, function (res) {
        console.log(res);

        $('#system-version').html(res.version);
        $('#system-runtime').html(res.runtime);
        $('#system-homeUrl').html(res.homeUrl);
        $('#system-userHome').html(res.userHome);
        $('#system-osName').html(res.osName);
        $('#system-osArch').html(res.osArch);
        $('#system-diskTotal').html(res.diskTotal);
        $('#system-diskFree').html(res.diskFree);
        $('#system-memoryTotal').html(res.memoryTotal);
        $('#system-memoryFree').html(res.memoryFree);
        $('#system-cpuCoreNum').html(res.cpuCoreNum);

        let config = res.config;
        $('#config-mountPath').val(config.mountPath);
        $('#config-storagePath').val(config.storagePath);
        $('#config-host').val(config.host);
        $('#config-port').val(config.port);
        $('#config-readBufferSize').val(config.readBufferSize);
        $('#config-writeBufferSize').val(config.writeBufferSize);
        $('#config-defaultLevel').val(config.defaultLevel);
        $('#config-loggerPath').val(config.loggerPath);
        $('#config-username').val(config.username);
        $('#config-password').val(config.password);

        let logInfoList = res.logInfoList;

        for (let i = 0; i < logInfoList.length; i++) {
            $('#log-tbody').append(
                '<tr>' +
                '<td>' + logInfoList[i].logTime + '</td>' +
                '<td>' + logInfoList[i].logPath + '</td>' +
                '<td>' +
                '<button type="button" class="btn btn-success-min open-log" data-path="' + logInfoList[i].logPath + '">打开</button>' +
                '<button type="button" class="btn btn-danger-min delete-log" data-path="' + logInfoList[i].logPath + '">删除</button>' +
                '</td>' +
                '</tr>'
            );
        }

        // 打开日志
        $('.open-log').click(function () {
            $.ajax(api, {
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    path: $(this).data('path'),
                    status: true
                })
            });
        });

        // 删除日志
        $('.delete-log').click(function () {
            $.ajax(api, {
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    path: $(this).data('path'),
                    status: false
                })
            });
            window.location.reload();
        });
    });
});